// 使用opencv模块部署yolov5-6.0版本

#include "yolo.h"
#include <iostream>
#include<opencv2/opencv.hpp>
#include<math.h>

using namespace std;
using namespace cv;
using namespace dnn;

int main()
{	
	cout<<"start-------------------"<<endl;
    string img_path = "../image/1.jpg";
	string model_path = "../model/best-sim.onnx";
	
	Yolo test;
	Net net;
	cout<<test.readModel(net, model_path, false)<<endl;
	if (test.readModel(net, model_path, false)) {
		cout << "read net ok!" << endl;
	}
	else {
		cout << "read net fail!" << endl;
		return -1;

	}

	//生成随机颜色
	vector<Scalar> color;
	srand(time(0));
	for (int i = 0; i < 80; i++) {
		int b = rand() % 256;
		int g = rand() % 256;
		int r = rand() % 256;
		color.push_back(Scalar(b, g, r));
	}
//------------图片检测--------------------
	vector<Output> result;
    vector<Output> result_sort_x;


	namedWindow("src",WINDOW_NORMAL);
	namedWindow("detect output",WINDOW_NORMAL);
    resizeWindow("detect output", 1280, 720);
    moveWindow("detect output", 1300, 0);
	Mat img = imread(img_path);
	if(img.empty()){
		cout<<"-----img loading failed!------"<<endl;
		return 0;
	}
	resize(img,img,Size(1280,720));
	imshow("src",img);
	if (test.Detect(img, net, result)) {             // 检测框
		// test.drawPred(img, result, color);
        result_sort_x = test.Sort(result);           // 框按框左上角排序，车位号都是按左到右的顺序
		test.drawPred(img, result_sort_x, color);	 // 显示检测出框在图像上，并打印结果

	}
	else {
		cout << "Detect Failed!"<<endl;
	}
    imshow("detect output",img);
	imwrite("../image/result.jpg",img);
	cout<<"saved success!"<<endl;
	waitKey(0);
	destroyAllWindows();
    return 0;
}

